<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>
<div id="app">
    <el-page-header style="background-color: #0095d7;color: white;line-height: 60px" @back="goBack" content="发布商品页面"></el-page-header>
    <el-divider></el-divider>
    <!--name代表上传文件时 文件的参数名
        limit="1" 设置只能选择一张图片
    -->
    <el-card>
        <el-form>

            <el-form-item label="商品标题" label-width="80px">
                <el-input v-model="product.title"></el-input>
            </el-form-item>

            <el-form-item label="价格" label-width="80px">
                <el-input v-model="product.price"></el-input>
            </el-form-item>

            <el-form-item label="原价" label-width="80px">
                <el-input v-model="product.oldPrice"></el-input>
            </el-form-item>

            <el-form-item label="销量" label-width="80px">
                <el-input v-model="product.saleCount"></el-input>
            </el-form-item>

            <el-form-item label="分类" label-width="80px">
                <template>
                    <el-select v-model="product.categoryId" placeholder="请选择">
                        <!--:label代表下拉选文字内容 :value代表文字内容对应的id 赋值给product.categoryId-->
                        <el-option v-for="c in categoryArr" :label="c.name" :value="c.id">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>

            <el-form-item label="商品图片" label-width="80px">
                <el-upload
                        action="/upload"
                        name="picFile"
                        :limit="1"
                        list-type="picture-card"
                        :on-preview="handlePictureCardPreview"
                        :on-success="handleSuccess"
                        :on-remove="handleRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>

        </el-form>

        <el-button style="margin: 10px 50%;background-color: #0095d7;color: white" @click="insert()">发布</el-button>

    </el-card>
</div>
</body>
<!-- import Vue before Element -->
<script src="js/vue.js"></script>
<!-- import JavaScript -->
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                product:{title:"",url:"",price:0,oldPrice:0,saleCount:0,categoryId:""},
                categoryArr:[]
            }
        },
        created: function (){
            axios.get("/category/select").then(function (response){
                v.categoryArr = response.data;
            })
        }
        ,
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                //当点击删除图片时方法会执行
                //file代表要删除的文件
                //file.response代表文件上传成功时 服务器响应的数据(文件名)
                console.log("文件名="+file.response);
                //http://localhost:8080/remove?name=xxx.jpg
                axios.get("/remove?name="+file.response).then(function (response) {
                    console.log("服务器图片已经删除")
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            insert(){
                //得到用户输入的微博文本内容和图片名 一起提交给服务器
                if (v.product.title==""||v.product.url==""||v.product.price==0||v.product.oldPrice==0){
                    alert("内容不能为空!")
                    return;
                }
                axios.post("/product/insert",v.product).then(function (response) {
                        alert("添加完成!");
                        //history.back(); 返回不会重新发请求 所以不会刷新页面
                        location.href="/admin.html"; //回到管理页面
                })
            },
            handleSuccess(response,file,fileList){
                //response=file.response
                console.log("文件上传完成, 图片名="+response);
                v.product.url = response;
            },
            goBack(){
                history.back();
            }
        }
    })
</script>
</html>